<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="外部id" prop="productOutid">
        <el-input
          v-model="queryParams.productOutid"
          placeholder="请输入外部id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="平台" prop="productPlatform">
        <el-input
          v-model="queryParams.productPlatform"
          placeholder="请输入平台"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="国家" prop="productCountry">
        <el-input
          v-model="queryParams.productCountry"
          placeholder="请输入国家"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="一级类目" prop="productTtCategory1">
        <el-input
          v-model="queryParams.productTtCategory1"
          placeholder="请输入一级类目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="二级类目" prop="productTtCategory2">
        <el-input
          v-model="queryParams.productTtCategory2"
          placeholder="请输入二级类目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="三级类目" prop="productTtCategory3">
        <el-input
          v-model="queryParams.productTtCategory3"
          placeholder="请输入三级类目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销量" prop="productSales">
        <el-input
          v-model="queryParams.productSales"
          placeholder="请输入销量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售额(本币)" prop="productGmvCurrentPrice">
        <el-input
          v-model="queryParams.productGmvCurrentPrice"
          placeholder="请输入销售额(本币)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售额(美金)" prop="productGmvUsd">
        <el-input
          v-model="queryParams.productGmvUsd"
          placeholder="请输入销售额(美金)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺的 id" prop="shopId">
        <el-input
          v-model="queryParams.shopId"
          placeholder="请输入店铺的 id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺外部ID" prop="shopOutid">
        <el-input
          v-model="queryParams.shopOutid"
          placeholder="请输入店铺外部ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="productBrand">
        <el-input
          v-model="queryParams.productBrand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新日期" prop="productInfoUpdateData">
        <el-date-picker clearable
          v-model="queryParams.productInfoUpdateData"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更新日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新用户" prop="productInfoUpdateBy">
        <el-input
          v-model="queryParams.productInfoUpdateBy"
          placeholder="请输入更新用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['social:SocialProduct:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['social:SocialProduct:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['social:SocialProduct:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['social:SocialProduct:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="SocialProductList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="外部id" align="center" prop="productOutid" />
      <el-table-column label="平台" align="center" prop="productPlatform" />
      <el-table-column label="国家" align="center" prop="productCountry" />
      <el-table-column label="链接" align="center" prop="productLink" />
      <el-table-column label="一级类目" align="center" prop="productTtCategory1" />
      <el-table-column label="二级类目" align="center" prop="productTtCategory2" />
      <el-table-column label="三级类目" align="center" prop="productTtCategory3" />
      <el-table-column label="商品名称" align="center" prop="productNameOrigin" />
      <el-table-column label="商品名称(文)" align="center" prop="productNameEn" />
      <el-table-column label="商品名称(中)" align="center" prop="productNameCn" />
      <el-table-column label="商品详情" align="center" prop="productDetailOrigin" />
      <el-table-column label="商品详情(英)" align="center" prop="productDetailEn" />
      <el-table-column label="商品详情(中)" align="center" prop="productDetailCn" />
      <el-table-column label="销量" align="center" prop="productSales" />
      <el-table-column label="销售额(本币)" align="center" prop="productGmvCurrentPrice" />
      <el-table-column label="销售额(美金)" align="center" prop="productGmvUsd" />
      <el-table-column label="店铺的 id" align="center" prop="shopId" />
      <el-table-column label="店铺外部ID" align="center" prop="shopOutid" />
      <el-table-column label="品牌" align="center" prop="productBrand" />
      <el-table-column label="更新日期" align="center" prop="productInfoUpdateData" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.productInfoUpdateData, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新用户" align="center" prop="productInfoUpdateBy" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['social:SocialProduct:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['social:SocialProduct:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="外部id" prop="productOutid">
          <el-input v-model="form.productOutid" placeholder="请输入外部id" />
        </el-form-item>
        <el-form-item label="平台" prop="productPlatform">
          <el-input v-model="form.productPlatform" placeholder="请输入平台" />
        </el-form-item>
        <el-form-item label="国家" prop="productCountry">
          <el-input v-model="form.productCountry" placeholder="请输入国家" />
        </el-form-item>
        <el-form-item label="链接" prop="productLink">
          <el-input v-model="form.productLink" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="一级类目" prop="productTtCategory1">
          <el-input v-model="form.productTtCategory1" placeholder="请输入一级类目" />
        </el-form-item>
        <el-form-item label="二级类目" prop="productTtCategory2">
          <el-input v-model="form.productTtCategory2" placeholder="请输入二级类目" />
        </el-form-item>
        <el-form-item label="三级类目" prop="productTtCategory3">
          <el-input v-model="form.productTtCategory3" placeholder="请输入三级类目" />
        </el-form-item>
        <el-form-item label="商品名称" prop="productNameOrigin">
          <el-input v-model="form.productNameOrigin" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商品名称(文)" prop="productNameEn">
          <el-input v-model="form.productNameEn" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商品名称(中)" prop="productNameCn">
          <el-input v-model="form.productNameCn" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商品详情" prop="productDetailOrigin">
          <el-input v-model="form.productDetailOrigin" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商品详情(英)" prop="productDetailEn">
          <el-input v-model="form.productDetailEn" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商品详情(中)" prop="productDetailCn">
          <el-input v-model="form.productDetailCn" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="销量" prop="productSales">
          <el-input v-model="form.productSales" placeholder="请输入销量" />
        </el-form-item>
        <el-form-item label="销售额(本币)" prop="productGmvCurrentPrice">
          <el-input v-model="form.productGmvCurrentPrice" placeholder="请输入销售额(本币)" />
        </el-form-item>
        <el-form-item label="销售额(美金)" prop="productGmvUsd">
          <el-input v-model="form.productGmvUsd" placeholder="请输入销售额(美金)" />
        </el-form-item>
        <el-form-item label="店铺的 id" prop="shopId">
          <el-input v-model="form.shopId" placeholder="请输入店铺的 id" />
        </el-form-item>
        <el-form-item label="店铺外部ID" prop="shopOutid">
          <el-input v-model="form.shopOutid" placeholder="请输入店铺外部ID" />
        </el-form-item>
        <el-form-item label="品牌" prop="productBrand">
          <el-input v-model="form.productBrand" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="更新日期" prop="productInfoUpdateData">
          <el-date-picker clearable
            v-model="form.productInfoUpdateData"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新用户" prop="productInfoUpdateBy">
          <el-input v-model="form.productInfoUpdateBy" placeholder="请输入更新用户" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSocialProduct, getSocialProduct, delSocialProduct, addSocialProduct, updateSocialProduct } from "@/api/social/SocialProduct";

export default {
  name: "SocialProduct",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品数据表格数据
      SocialProductList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productOutid: null,
        productPlatform: null,
        productCountry: null,
        productLink: null,
        productTtCategory1: null,
        productTtCategory2: null,
        productTtCategory3: null,
        productNameOrigin: null,
        productNameEn: null,
        productNameCn: null,
        productDetailOrigin: null,
        productDetailEn: null,
        productDetailCn: null,
        productSales: null,
        productGmvCurrentPrice: null,
        productGmvUsd: null,
        shopId: null,
        shopOutid: null,
        productBrand: null,
        productInfoUpdateData: null,
        productInfoUpdateBy: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productOutid: [
          { required: true, message: "外部id不能为空", trigger: "blur" }
        ],
        productPlatform: [
          { required: true, message: "平台不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品数据列表 */
    getList() {
      this.loading = true;
      listSocialProduct(this.queryParams).then(response => {
        this.SocialProductList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        productOutid: null,
        productPlatform: null,
        productCountry: null,
        productLink: null,
        productTtCategory1: null,
        productTtCategory2: null,
        productTtCategory3: null,
        productNameOrigin: null,
        productNameEn: null,
        productNameCn: null,
        productDetailOrigin: null,
        productDetailEn: null,
        productDetailCn: null,
        productSales: null,
        productGmvCurrentPrice: null,
        productGmvUsd: null,
        shopId: null,
        shopOutid: null,
        productBrand: null,
        productInfoUpdateData: null,
        productInfoUpdateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSocialProduct(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSocialProduct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSocialProduct(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品数据编号为"' + ids + '"的数据项？').then(function() {
        return delSocialProduct(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('social/SocialProduct/export', {
        ...this.queryParams
      }, `SocialProduct_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
